package cn.dzhang.rcp.hyperbola.adapters;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.IWorkbenchAdapter2;
import org.eclipse.ui.model.IWorkbenchAdapter3;
import org.eclipse.ui.model.WorkbenchAdapter;

import cn.dzhang.rcp.hyperbola.common.SWTResourceManager;
import cn.dzhang.rcp.hyperbola.common.UIConstants;
import cn.dzhang.rcp.hyperbola.common.UIUtils;
import cn.dzhang.rcp.hyperbola.model.ContactEntry;
import cn.dzhang.rcp.hyperbola.model.ContactGroup;
import cn.dzhang.rcp.hyperbola.model.Presence;

@SuppressWarnings("rawtypes")
public class ContactApdapterFactory implements IAdapterFactory {
	public static final Map<Presence, String> ImageMap = new HashMap<Presence, String>();

	static {
		ImageMap.put(Presence.AWAY, UIConstants.ImagePaths.AWAY);
		ImageMap.put(Presence.DO_NOT_DISTURB,
				UIConstants.ImagePaths.DO_NOT_DISTURB);
		ImageMap.put(Presence.INVISIBLE, UIConstants.ImagePaths.OFFLINE);
		ImageMap.put(Presence.ONLINE, UIConstants.ImagePaths.ONLINE);
	}
	private static final IWorkbenchAdapter groupAdapter = new WorkbenchAdapter() {
		@Override
		public RGB getForeground(Object element) {
			return SWTResourceManager.getColor(SWT.COLOR_BLUE).getRGB();
		}

		@Override
		public RGB getBackground(Object element) {
			return SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND)
					.getRGB();
		}

		@Override
		public Object getParent(Object o) {
			return ((ContactGroup) o).getParent();
		}

		@Override
		public String getLabel(Object o) {
			return ((ContactGroup) o).getName();
		}

		@Override
		public ImageDescriptor getImageDescriptor(Object object) {
			return UIUtils.getImageDescriptor(UIConstants.ImagePaths.GROUP);
		}

		@Override
		public Object[] getChildren(Object o) {
			return ((ContactGroup) o).getEntries().toArray();
		}
	};
	private static final IWorkbenchAdapter entryAdapter = new WorkbenchAdapter() {

		@Override
		public Object getParent(Object o) {
			return ((ContactEntry) o).getParent();
		}

		@Override
		public String getLabel(Object o) {
			return ((ContactEntry) o).getName();
		}

		@Override
		public ImageDescriptor getImageDescriptor(Object object) {
			ContactEntry entry = (ContactEntry) object;
			String key = ImageMap.get(entry.getPresence());

			return UIUtils.getImageDescriptor(key);
		}

		@Override
		public Object[] getChildren(Object o) {
			return new Object[0];
		}
	};

	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType != IWorkbenchAdapter.class
				&& adapterType != IWorkbenchAdapter2.class
				&& adapterType != IWorkbenchAdapter3.class) {
			return null;
		} else {
			if (adaptableObject instanceof ContactGroup) {
				return groupAdapter;
			} else if (adaptableObject instanceof ContactEntry) {
				return entryAdapter;
			} else {
				return null;
			}
		}
	}

	@Override
	public Class[] getAdapterList() {
		return new Class<?>[] { IWorkbenchAdapter.class,
				IWorkbenchAdapter2.class, IWorkbenchAdapter3.class };
	}
}
